﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using He.Common;

namespace He.Web.manage.settings
{
    public partial class sys_img_config : He.Web.UI.Manage
    {
        private int fileSize = 50 * 1024;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                chkLoginLevel("sys_img_config");
                ShowInfo();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            He.BLL.webset bll = new BLL.webset();
            He.Model.webset model = bll.GetWebset();
            attachimgmaxwidth.Text = model.attachimgmaxwidth.ToString();
            attachimgmaxheight.Text = model.attachimgmaxheight.ToString();
            watermarktype.SelectedValue = model.watermarktype.ToString();
            watermarkposition.Text = model.watermarkposition.ToString();
            watermarkimgquality.Text = model.watermarkimgquality.ToString();
            if (!string.IsNullOrEmpty(model.watermarkpic) && File.Exists(PathHelper.MapPath(model.watermarkpic)))
            {
                watermarkpic_review.Text = string.Format("<img src=\"{0}\" height=\"30\">", model.watermarkpic);
            }
            watermarktransparency.Text = model.watermarktransparency.ToString();
            watermarktext.Text = model.watermarktext;
            watermarkfont.Text = model.watermarkfont;
            watermarkfontsize.Text = model.watermarkfontsize.ToString();
        }
        #endregion

        /// <summary>
        /// 保存配置信息
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLL.webset bll = new BLL.webset();
            Model.webset model = bll.GetWebset();

            model.attachimgmaxwidth = int.Parse(attachimgmaxwidth.Text.Trim());
            model.attachimgmaxheight = int.Parse(attachimgmaxheight.Text.Trim());
            model.watermarktype = int.Parse(watermarktype.SelectedValue);
            model.watermarkposition = int.Parse(watermarkposition.Text.Trim());
            model.watermarkimgquality = int.Parse(watermarkimgquality.Text.Trim());

            HttpPostedFile _postedFile = watermarkpic.PostedFile;
            if (_postedFile.FileName != string.Empty)
            {
                string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(UploadImgExt, _fileExt))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>不允许上传" + _fileExt + "类型的文件,请重试。", "back", "Error");
                    return;
                }
                if (_postedFile.ContentLength > fileSize)
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>图片超过50K，请先压缩图片。", "back", "Error");
                    return;
                }
                FileHelper.DeleteFile(webset.web_path + model.watermarkpic);
                _postedFile.SaveAs(Server.MapPath("~/") + "watermark." + _fileExt);
                model.watermarkpic = webset.web_path + "watermark." + _fileExt;
            }
            model.watermarktransparency = int.Parse(watermarktransparency.Text.Trim());
            model.watermarktext = watermarktext.Text;
            model.watermarkfont = watermarkfont.Text;
            model.watermarkfontsize = int.Parse(watermarkfontsize.Text.Trim());
            bll.SaveConfig(model);
            SaveLogs("[系统管理]系统图片设置");
            JscriptPrint("配置修改成功啦！", "sys_img_config.aspx", "Success");
        }
    }
}